﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Honestysoft.Baseframework.WebUI;
using Honestysoft.BaseObject;
using System.Data;

namespace Honestysoft.Financing.WebUI.Module.Contract
{
    public partial class ContractConfirmList : BasePage
    {
        protected override void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);
            if (!IsPostBack)
            {
                AspNetPager1.PageSize = Globals.PageSize;
                //BindControl();
                Bind();
            }
        }
        /// <summary>
        /// 绑定页面控件。
        /// </summary>
        private void BindControl()
        {
            ControlHelper.BindDictionary(ddlContractCategory, "ContractCategory");
            ControlHelper.InsertAllItemAndSelect(ref ddlContractCategory);
            Utils.ContractTreeNodeType nodeType = (Utils.ContractTreeNodeType)int.Parse(Request.QueryString[Utils.ParamName_ContractNodeType]);
            switch (nodeType)
            {
                case Utils.ContractTreeNodeType.Root:
                    break;
                case Utils.ContractTreeNodeType.ProjectCategory://项目分类
                    break;
                case Utils.ContractTreeNodeType.Project://项目
                    //txtProjectName.Text = new Honestysoft.Financing.BusinessLogic.Project(Globals.CurrentConnStringName).GetProjectName(Request.QueryString["ID"]);
                    txtProjectName.Text = Request.QueryString["NodeText"].Split(' ')[0];
                    txtProjectName.Enabled = false;
                    break;
                case Utils.ContractTreeNodeType.ContractCategory://合同类别
                    txtProjectName.Text = new Honestysoft.Financing.BusinessLogic.Project(Globals.CurrentConnStringName).GetProjectName(Request.QueryString["ParentID"]);
                    txtProjectName.Enabled = false;
                    ddlContractCategory.SelectedValue = Request.QueryString["ID"];
                    ddlContractCategory.Enabled = false;
                    break;
                case Utils.ContractTreeNodeType.Contract:
                    break;
                default:
                    break;
            }
        }
        /// <summary>
        /// 设置弹出窗体大小
        /// 基类中已自动做控制。默认宽度800，高度600
        /// </summary>
        protected override void SetDialogSize()
        {
            //base.SetDialogSize();
            //重载宽度，高度
            ModalDialogSize.EditHeight = 500;
            ModalDialogSize.EditWidth = 600;
            ModalDialogSize.ViewHeight = 500;
            ModalDialogSize.ViewWidth = 600;
        }
        /// <summary>
        /// 根据权限控制页面按钮.
        /// 基类中已自动做控制。
        /// </summary>
        protected override void ButtonSetting()
        {
            base.ButtonSetting();
            //使用PageSecurity检查权限
            if (PageSecurity.Add)
            {
                string projectId = "", category = "";
                Utils.ContractTreeNodeType nodeType = (Utils.ContractTreeNodeType)int.Parse(Request.QueryString[Utils.ParamName_ContractNodeType]);
                switch (nodeType)
                {
                    case Utils.ContractTreeNodeType.Project://项目
                        projectId = Request.QueryString["ID"];
                        category = "0";
                        break;
                    case Utils.ContractTreeNodeType.ContractCategory://合同类别
                        projectId = Request.QueryString["ParentID"];
                        category = Request.QueryString["ID"];
                        break;
                    default:
                        break;
                }
                //btnAdd.Attributes.Add("onclick", "javascript:openDialog('" + Globals.Home + "Module/Contract/BD_Contract_Edit.aspx?ProjectID=" + projectId + "&ContractCategory=" + category + "&FunctionID=" + FunctionID + "'," + ModalDialogSize.EditWidth + ", " + ModalDialogSize.EditHeight + ", '新增基础资料-合同信息');return false;");
            }
        }
        /// <summary>
        /// 得到查询参数
        /// </summary>
        /// <returns>查询参数</returns>
        private QueryParameters GetQueryParameters()
        {
            QueryParameters queryParameters = new QueryParameters();


            if (ddlContractCategory.SelectedValue != "0")
            {
                queryParameters.Add("ContractCategory", ddlContractCategory.SelectedValue, QueryParameters.OperatorType.Equal);
            }
            if (txtCode.Text.Trim().Length > 0)
            {
                queryParameters.Add("Code", txtCode.Text, QueryParameters.OperatorType.Like);
            }
            if (txtName.Text.Trim().Length > 0)
            {
                queryParameters.Add("Name", txtName.Text, QueryParameters.OperatorType.Like);
            }
            return queryParameters;
        }
        /// <summary>
        /// 得到排序参数
        /// </summary>
        /// <returns></returns>
        private OrderParameters GetOrderParameters()
        {
            OrderParameters orderParameters = new OrderParameters();
            orderParameters.Add("Code");
            //添加排序列
            return orderParameters;
        }


        protected string GetCheckbox(string dataId, bool isInit)
        {
            if (isInit) { return ""; }
            return "<input type=\"checkbox\" name=\"chkID\" id=\"chk" + dataId + "\"  value=\"" + dataId + "\" />";
        }

        /// <summary>
        /// 绑定列表
        /// </summary>
        private void Bind()
        {
            Honestysoft.Financing.BusinessLogic.Contract bll = new BusinessLogic.Contract(Globals.CurrentConnStringName);
            Utils.ContractTreeNodeType nodeType = (Utils.ContractTreeNodeType)int.Parse(Request.QueryString[Utils.ParamName_ContractNodeType]);
            string projectCategory = "";
            string projectId = "", projectName = "";
            string contractCategory = ddlContractCategory.SelectedValue;
            if (contractCategory == "0") { contractCategory = null; }
            string code = txtCode.Text.Trim();
            if (code.Length == 0) { code = null; }
            string name = txtName.Text.Trim();
            if (name.Length == 0) { name = null; }
            if (txtProjectName.Enabled)
            {
                if (txtProjectName.Text.Trim().Length > 0)
                {
                    projectName = txtProjectName.Text.Trim();
                }
            }
            switch (nodeType)
            {
                case Utils.ContractTreeNodeType.Root:
                    break;
                case Utils.ContractTreeNodeType.ProjectCategory://项目分类
                    projectCategory = Request.QueryString["ID"];
                    projectId = null;
                    break;
                case Utils.ContractTreeNodeType.Project://项目
                    projectCategory = Request.QueryString["ParentID"];
                    projectId = Request.QueryString["ID"];
                    break;
                case Utils.ContractTreeNodeType.ContractCategory://合同类别
                    projectId = Request.QueryString["ParentID"];
                    contractCategory = Request.QueryString["ID"];
                    break;
                default:
                    break;
            }

            int pageCount = 0, recordCount = 0;
            DataTable dtList = bll.GetContracts(projectCategory, projectId, projectName, contractCategory, code, name, AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex, out pageCount, out recordCount);
            AspNetPager1.RecordCount = recordCount;
            rptList.DataSource = dtList;
            rptList.DataBind();
        }
        /// <summary>
        /// 得到表格行操作按钮脚本
        /// </summary>
        /// <param name="dataId">记录ID</param>
        /// <returns>操作按钮脚本</returns>
        protected string GetButtonScript(string dataId, bool isInit)
        {
            string script = "";
            //if (PageSecurity.Edit)
            //{
            //    script += ScriptHelper.LinkButton.GetGridEditButton(Globals.Home + "Module/Contract/BD_Contract_Edit.aspx", "修改基础资料-合同信息", ModalDialogSize.EditWidth, ModalDialogSize.EditHeight, dataId, FunctionID);
            //}
            //if (PageSecurity.Delete && !isInit)
            //{
            //    if (script.Length > 0) { script += "&nbsp;"; }
            //    script += ScriptHelper.LinkButton.GetGridDeleteButton(dataId, btnDelete.ClientID);
            //}
            if (PageSecurity.View)
            {
                //if (script.Length > 0) { script += "&nbsp;"; }

                script = "<a href='ContractConfirmInfo.aspx?ID=" + dataId + "&FunctionID="+FunctionID+"'><img src='" + ScriptHelper.Image.GetUrl(ScriptHelper.Image.ImageType.Edit) + "' border='0' alt='点击查看合同信息' /></a>";

               // script += ScriptHelper.LinkButton.GetGridViewButton(Globals.Home + "Module/Contract/BD_Contract_View.aspx", "查看基础资料-合同信息", ModalDialogSize.ViewWidth, ModalDialogSize.ViewHeight, dataId, FunctionID);
            }
            return script;
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            Bind();
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            Bind();
        }
        /// <summary>
        /// 得到选中的记录ID集合
        /// </summary>
        /// <returns></returns>
        private string[] GetSelectedRowKeys()
        {
            string strId = Request["chkID"] + "";
            if (string.IsNullOrEmpty(strId)) { return null; }
            return strId.Split(',');
        }

    }
}
